package com.hnl.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hnl.core.aware.MiscTypeAware;
import com.hnl.dao.DAOException;
import com.hnl.dao.HNLDAOFactory;
import com.hnl.dao.MiscDAO;
import com.hnl.util.JSONResponseUtil;

@SuppressWarnings("serial")
public class PopulateDropdownServlet extends HttpServlet implements
		MiscTypeAware {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter writer = resp.getWriter();
		try {
			String typeName = req.getParameter(PARAM_TYPE_NAME);
			String isNA = req.getParameter("isNA");
			if (isNA == null) isNA="YES";
			MiscDAO miscDAO = HNLDAOFactory.getDAOFactory(
					HNLDAOFactory.HNLDAOFACTORY).getMiscDAO();
			Map<Integer, String> miscMap = null;

			if (isNA.matches("NO")){
				miscMap = miscDAO.loadMiscNameByMiscTypeName(typeName);
			}else{
				miscMap =  miscDAO.loadMiscNameByMiscTypeName(DEFAULT_MISC);
				Map<Integer, String> miscNameMap = miscDAO.loadMiscNameByMiscTypeName(typeName);		
				miscMap.putAll(miscNameMap);
			}
			
			writer.println(JSONResponseUtil.getJSONObjectString(miscMap));
		} catch (DAOException e) {
			e.printStackTrace();
		} finally{
			writer.close();
		}
	}

}
